package com.guofen.controller.name;

import com.guofen.pojo.Name;
import com.guofen.service.name.NameService;
import com.guofen.util.result.Result;
import com.guofen.util.result.ResultGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RequestMapping("/name")
@RestController
@Api(tags = "NameController", description = "UTA assignment")
public class NameController {

    @Autowired
    private NameService service;

    @Value("${user.filepath}")
    private String path;

    @ApiOperation("get all names")
    @RequestMapping(value = "/getAllName",method = RequestMethod.POST)
    public Result getAllName(@RequestParam Integer pageSize,
                             @RequestParam Integer currentPage) {
        return ResultGenerator.genSuccessResult(service.getAllName(pageSize, currentPage));
    }

    @ApiOperation("get by name")
    @RequestMapping(value = "/getByName",method = RequestMethod.POST)
    public Result getByName(@RequestParam String name,
                            @RequestParam Integer pageSize,
                            @RequestParam Integer currentPage) {
        return ResultGenerator.genSuccessResult(service.getByName(name, pageSize, currentPage));
    }

    @ApiOperation("get by grade1")
    @RequestMapping(value = "/getByGrade1",method = RequestMethod.POST)
    public Result getByGrade1(@RequestParam Integer grade1,
                             @RequestParam Integer grade2,
                             @RequestParam Integer pageSize,
                             @RequestParam Integer currentPage) {
        return ResultGenerator.genSuccessResult(service.getByGrade1(grade1, grade2, pageSize, currentPage));
    }

    @ApiOperation("get by grade2")
    @RequestMapping(value = "/getByGrade2",method = RequestMethod.POST)
    public Result getByGrade2(@RequestParam Integer grade1,
                             @RequestParam Integer grade2,
                             @RequestParam Integer pageSize,
                             @RequestParam Integer currentPage) {
        return ResultGenerator.genSuccessResult(service.getByGrade2(grade1, grade2, pageSize, currentPage));
    }

    @ApiOperation("get by grade3")
    @RequestMapping(value = "/getByGrade3",method = RequestMethod.POST)
    public Result getByGrade3(@RequestParam Integer grade1,
                             @RequestParam Integer grade2,
                             @RequestParam Integer pageSize,
                             @RequestParam Integer currentPage) {
        return ResultGenerator.genSuccessResult(service.getByGrade3(grade1, grade2, pageSize, currentPage));
    }

    @ApiOperation("delete")
    @PostMapping("/delete")
    public Result delete(@RequestParam("name") String name) {
        return ResultGenerator.genSuccessResult(service.delete(name));
    }

    @ApiOperation("upload")
    @PostMapping("/upload")
    public Result upload(@RequestParam("name") String name,
                         @RequestParam("grade1") Integer grade1,
                         @RequestParam("grade2") Integer grade2,
                         @RequestParam("grade3") Integer grade3,
                         @RequestParam("major") String major,
                         @RequestParam("honor") String honor){
        Name name1 = service.getByName(name);
        name1.setName(name);
        name1.setGrade1(grade1);
        name1.setGrade2(grade2);
        name1.setGrade3(grade3);
        name1.setMajor(major);
        name1.setHonor(honor);
        return ResultGenerator.genSuccessResult(service.update(name1));
    }

    @ApiOperation("upload file")
    @PostMapping("/uploadFile")
    public Result uploadFile(@RequestParam String name,
                             @RequestParam MultipartFile file){
        Name name1 = service.getByName(name);
        if(file == null || file.isEmpty()){
            return ResultGenerator.genFailResult("file not exist");
        }
        String originalFileName = file.getOriginalFilename();
        String fileName = path + originalFileName;

        name1.setPicture(originalFileName);

        File tempFile = new File(fileName);
        try {
            file.transferTo(tempFile);
            service.update(name1);
        }catch(IOException e){
            e.printStackTrace();
            return ResultGenerator.genFailResult("failed");
        }
        return ResultGenerator.genSuccessResult();
    }

    @ApiOperation("insert")
    @PostMapping("/add")
    public Result add(@RequestParam("name") String name,
                      @RequestParam("grade1") Integer grade1,
                      @RequestParam("grade2") Integer grade2,
                      @RequestParam("grade3") Integer grade3,
                      @RequestParam("major") String major,
                      @RequestParam("honor") String honor){
        Name name1 = service.getByName(name);
        if (name1 != null)
            return ResultGenerator.genFailResult("name has existed");
        name1 = new Name();
        name1.setName(name);
        name1.setGrade1(grade1);
        name1.setGrade2(grade2);
        name1.setGrade3(grade3);
        name1.setMajor(major);
        name1.setHonor(honor);
        return ResultGenerator.genSuccessResult(service.insert(name1));
    }
}
